package com.monitoring.interfaces.controller;

import com.monitoring.application.dto.DeviceDTO;
import com.monitoring.application.service.DeviceApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/devices")
public class DeviceController {

    private final DeviceApplicationService deviceApplicationService;

    @Autowired
    public DeviceController(DeviceApplicationService deviceApplicationService) {
        this.deviceApplicationService = deviceApplicationService;
    }

    // 创建新设备
    @PostMapping
    public ResponseEntity<DeviceDTO> createDevice(@RequestBody DeviceDTO deviceDTO) {
        DeviceDTO createdDevice = deviceApplicationService.createDevice(deviceDTO);
        return new ResponseEntity<>(createdDevice, HttpStatus.CREATED);
    }

    // 根据ID获取设备
    @GetMapping("/{id}")
    public ResponseEntity<DeviceDTO> getDeviceById(@PathVariable Long id) {
        return deviceApplicationService.getDeviceById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // 获取所有设备
    @GetMapping
    public ResponseEntity<List<DeviceDTO>> getAllDevices() {
        List<DeviceDTO> devices = deviceApplicationService.getAllDevices();
        return ResponseEntity.ok(devices);
    }

    // 根据类型获取设备
    @GetMapping("/type/{type}")
    public ResponseEntity<List<DeviceDTO>> getDevicesByType(@PathVariable String type) {
        List<DeviceDTO> devices = deviceApplicationService.getDevicesByType(type);
        return ResponseEntity.ok(devices);
    }

    // 根据位置获取设备
    @GetMapping("/location/{location}")
    public ResponseEntity<List<DeviceDTO>> getDevicesByLocation(@PathVariable String location) {
        List<DeviceDTO> devices = deviceApplicationService.getDevicesByLocation(location);
        return ResponseEntity.ok(devices);
    }

    // 获取活跃设备
    @GetMapping("/active")
    public ResponseEntity<List<DeviceDTO>> getActiveDevices() {
        List<DeviceDTO> activeDevices = deviceApplicationService.getActiveDevices();
        return ResponseEntity.ok(activeDevices);
    }

    // 控制设备开关
    @PutMapping("/{id}/power")
    public ResponseEntity<DeviceDTO> toggleDevicePower(@PathVariable Long id, @RequestBody Map<String, Boolean> request) {
        Boolean poweredOn = request.get("poweredOn");
        if (poweredOn == null) {
            return ResponseEntity.badRequest().build();
        }
        try {
            DeviceDTO updatedDevice = poweredOn ? 
                deviceApplicationService.turnOnDevice(id) : 
                deviceApplicationService.turnOffDevice(id);
            return ResponseEntity.ok(updatedDevice);
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 激活/停用设备
    @PutMapping("/{id}/active")
    public ResponseEntity<DeviceDTO> toggleDeviceActive(@PathVariable Long id, @RequestBody Map<String, Boolean> request) {
        Boolean active = request.get("active");
        if (active == null) {
            return ResponseEntity.badRequest().build();
        }
        try {
            DeviceDTO updatedDevice = active ? 
                deviceApplicationService.activateDevice(id) : 
                deviceApplicationService.deactivateDevice(id);
            return ResponseEntity.ok(updatedDevice);
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 切换设备维护状态
    @PutMapping("/{id}/maintenance")
    public ResponseEntity<DeviceDTO> toggleDeviceMaintenance(@PathVariable Long id, @RequestBody Map<String, Boolean> request) {
        Boolean inMaintenance = request.get("inMaintenance");
        if (inMaintenance == null) {
            return ResponseEntity.badRequest().build();
        }
        try {
            DeviceDTO updatedDevice = inMaintenance ? 
                deviceApplicationService.putDeviceInMaintenance(id) : 
                deviceApplicationService.resumeDeviceOperation(id);
            return ResponseEntity.ok(updatedDevice);
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 删除设备
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDevice(@PathVariable Long id) {
        try {
            deviceApplicationService.deleteDevice(id);
            return ResponseEntity.noContent().build();
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }
}